/**
 * Copyright 2025   indonesia_qs_driver
 * <p>
 * 一句话功能简述
 *
 *
 * @author wzp_vicky   623301600@qq.com
 * @version 1.0      2025/9/11 15:24
 * @description:源自flutter_upgrader_indicator.dart，主要是实现进度在按钮以上的区域显示另外那个是从按钮下面开始的
 *
 *
 */
import 'package:flutter/material.dart';
import 'package:flutter_upgrader/flutter_upgrader_wave.dart';

class LiquidProgressIndicator extends ProgressIndicator {
  final Axis direction;
  final Color? borderColor;
  final double? borderWidth;
  final double? borderRadius; // 现在仅控制顶部圆角大小
  final Widget? center;

  const LiquidProgressIndicator({
    super.key,
    double super.value = 0.5,
    super.backgroundColor,
    Animation<Color>? super.valueColor,
    this.direction = Axis.horizontal,
    this.borderWidth,
    this.borderColor,
    this.borderRadius,
    this.center,
  });

  Color getBackgroundColor(BuildContext context) {
    return backgroundColor ?? const Color(0x0000BFFF);
  }

  Color getValueColor(BuildContext context) {
    return valueColor?.value ?? const Color(0x6600BFFF);
  }

  @override
  LiquidProgressIndicatorState createState() => LiquidProgressIndicatorState();
}

class LiquidProgressIndicatorState extends State<LiquidProgressIndicator> {
  @override
  Widget build(BuildContext context) {
    final useBorderColor = widget.borderColor != null;
    final useBorderWidth = widget.borderWidth != null;

    return ClipPath(
      // 1. 修改裁剪器：仅顶部圆角
      clipper: LinearClipper(
        topRadius: widget.borderRadius, // 传入顶部圆角大小
      ),
      child: CustomPaint(
        // 2. 修改背景绘制：仅顶部圆角
        painter: LinearPainter(
          color: widget.getBackgroundColor(context),
          topRadius: widget.borderRadius,
        ),
        // 3. 修改边框绘制：仅顶部圆角
        foregroundPainter: useBorderColor && useBorderWidth
            ? LinearBorderPainter(
          color: widget.borderColor!,
          width: widget.borderWidth!,
          topRadius: widget.borderRadius,
        )
            : null,
        child: Stack(
          children: <Widget>[
            Wave(
              value: widget.value!,
              color: widget.getValueColor(context),
              direction: widget.direction,
            ),
            widget.center != null ? Center(child: widget.center) : Container(),
          ],
        ),
      ),
    );
  }
}

// -------------------------- 关键修改：仅顶部圆角裁剪器 --------------------------
class LinearClipper extends CustomClipper<Path> {
  final double? topRadius; // 仅控制顶部圆角

  LinearClipper({this.topRadius});

  @override
  Path getClip(Size size) {
    final radiusValue = topRadius ?? 0.0; // 圆角默认值（无圆角）
    final path = Path();

    // 1. 起点：左上角外侧（先预留圆角空间）
    path.moveTo(0, radiusValue);
    // 2. 顶部左侧圆角：从(0, radius)到(radius, 0)的圆弧
    path.arcToPoint(
      Offset(radiusValue, 0),
      radius: Radius.circular(radiusValue),
      clockwise: false,
    );
    // 3. 顶部直线：从右上角到左上角（水平向右）
    path.lineTo(size.width - radiusValue, 0);
    // 4. 顶部右侧圆角：从(size.width - radius, 0)到(size.width, radius)的圆弧
    path.arcToPoint(
      Offset(size.width, radiusValue),
      radius: Radius.circular(radiusValue),
      clockwise: false,
    );
    // 5. 右侧直线：从右上角垂直向下到右下角（无圆角）
    path.lineTo(size.width, size.height);
    // 6. 底部直线：从右下角水平向左到左下角（无圆角）
    path.lineTo(0, size.height);
    // 7. 左侧直线：从左下角垂直向上到起点（无圆角）
    path.close();

    return path;
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) {
    // 仅当顶部圆角变化时，重新裁剪
    if (oldClipper is LinearClipper) {
      return topRadius != oldClipper.topRadius;
    }
    return false;
  }
}

// -------------------------- 关键修改：仅顶部圆角背景绘制 --------------------------
class LinearPainter extends CustomPainter {
  final Color color;
  final double? topRadius; // 仅控制顶部圆角

  LinearPainter({required this.color, this.topRadius});

  @override
  void paint(Canvas canvas, Size size) {
    final radiusValue = topRadius ?? 0.0;
    final paint = Paint()..color = color;

    // 绘制“仅顶部圆角”的矩形（RRect：圆角矩形）
    final rRect = RRect.fromRectAndCorners(
      Rect.fromLTWH(0, 0, size.width, size.height), // 整个控件的矩形范围
      topLeft: Radius.circular(radiusValue), // 顶部左侧圆角
      topRight: Radius.circular(radiusValue), // 顶部右侧圆角
      bottomLeft: Radius.circular(0), // 底部左侧无圆角
      bottomRight: Radius.circular(0), // 底部右侧无圆角
    );

    canvas.drawRRect(rRect, paint);
  }

  @override
  bool shouldRepaint(LinearPainter oldDelegate) {
    return color != oldDelegate.color || topRadius != oldDelegate.topRadius;
  }
}

// -------------------------- 关键修改：仅顶部圆角边框绘制 --------------------------
class LinearBorderPainter extends CustomPainter {
  final Color color;
  final double width;
  final double? topRadius; // 仅控制顶部圆角

  LinearBorderPainter({
    required this.color,
    required this.width,
    this.topRadius,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final radiusValue = topRadius ?? 0.0;
    final paint = Paint()
      ..color = color
      ..style = PaintingStyle.stroke
      ..strokeWidth = width;

    // 边框需要“向内偏移”（避免边框一半超出控件范围）
    final borderOffset = width / 2;
    // 绘制“仅顶部圆角”的边框矩形
    final borderRRect = RRect.fromRectAndCorners(
      Rect.fromLTWH(
        borderOffset, // 左偏移（边框宽度的一半）
        borderOffset, // 上偏移（边框宽度的一半）
        size.width - borderOffset * 2, // 边框宽度方向：减去两侧偏移
        size.height - borderOffset * 2, // 边框高度方向：减去两侧偏移
      ),
      topLeft: Radius.circular(radiusValue), // 顶部左侧圆角
      topRight: Radius.circular(radiusValue), // 顶部右侧圆角
      bottomLeft: Radius.circular(0), // 底部左侧无圆角
      bottomRight: Radius.circular(0), // 底部右侧无圆角
    );

    canvas.drawRRect(borderRRect, paint);
  }

  @override
  bool shouldRepaint(LinearBorderPainter oldDelegate) {
    return color != oldDelegate.color ||
        width != oldDelegate.width ||
        topRadius != oldDelegate.topRadius;
  }
}